package com.tourism.website.articlemanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tourism.website.articlemanage.entity.Article;
import com.tourism.website.articlemanage.mapper.ArticleCategoryMapper;
import com.tourism.website.articlemanage.mapper.ArticleImgMapper;
import com.tourism.website.articlemanage.mapper.ArticleMapper;
import com.tourism.website.articlemanage.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourismwebsite.articlemanage.dto.ArticleManageDTO;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kevin
 * @since 2024-06-11
 */
@Mapper(componentModel="spring")
interface MsArticleManageMapper{
    ArticleManageDTO ArticleToArticleManageDTO(Article article);
}
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Resource
    MsArticleManageMapper msArticleManageMapper;
    @Resource
    ArticleMapper articleMapper;
    @Resource
    ArticleImgMapper articleImgMapper;
    @Resource
    ArticleCategoryMapper articleCategoryMapper;
    //0通过，1不通过，2审批中
    @Override
    public Boolean updateByArticleId(Integer articleId,String status,Integer reviewer) {
        int result = articleMapper.updateFavoriteStatusByArticleId(articleId,status,reviewer);
        int result1 = articleMapper.updateTime(articleId, LocalDateTime.now(),LocalDateTime.now());
        return result > 0 && result1 >0;
    }

    //查询文章内容
    @Override
    public List<ArticleManageDTO> queryArticleManage(Integer articleId) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Article::getArticleId,Article::getTitle,Article::getUserName,Article::getNickName,
                Article::getReviewer,Article::getStatus,Article::getDelFlag,
                Article::getCreateTime,Article::getUpdateTime,Article::getReviewerTime,
                Article::getUserId,Article::getContent);
        wrapper.eq(Article::getArticleId,articleId);
        List<Article> articles = baseMapper.selectList(wrapper);
        List<ArticleManageDTO> articleManageDTOS = articles.stream()
                .map(Article -> msArticleManageMapper.ArticleToArticleManageDTO(Article))
                .collect(Collectors.toList());
        return articleManageDTOS;
    }

    @Override
    public Boolean deleteArticleManage(Integer articleId) {
        int result = articleMapper.deleteArticleManage(articleId);
        int result1 = articleCategoryMapper.deleteArticleManage(articleId);
        return result > 0 && result1>0;
    }

    @Override
    public List<ArticleManageDTO> queryNameAndTime(Integer articleId) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Article::getCreateTime,Article::getTitle,Article::getStatus);
        wrapper.eq(Article::getArticleId,articleId);
        List<Article> articles = baseMapper.selectList(wrapper);
        List<ArticleManageDTO> articleManageDTOS = articles.stream()
                .map(Article -> msArticleManageMapper.ArticleToArticleManageDTO(Article))
                .collect(Collectors.toList());
        return articleManageDTOS;
    }
}